import { Controller, UseGuards } from '@nestjs/common';
import { Crud, CrudController, Override, ParsedRequest, ParsedBody, CrudRequest, CrudAuth } from '@nestjsx/crud';
import { ApiTags, ApiBearerAuth, ApiBody} from '@nestjs/swagger';
import { SearchHistory } from '../../entities/search_history.entities';
import { SearchHistoryService } from '../../services/search-history/search-history.service';
import { AuthGuard } from '@nestjs/passport';
import * as moment from 'moment';

@Crud({
  model:{
    type:SearchHistory,
  },
  query:{
    join:{
      user:{
        exclude:[]
      }
    },
    exclude:["user_id"]
  },
  routes:{
    only:['createOneBase','getManyBase']
  }
})
@CrudAuth({
  property:"user",
  filter: (user) => ({
    user_id: user.userId
  }),
  persist:(user) => {
    return user;
  }
})
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiTags('search-history')
@Controller('api/search-history')
export class SearchHistoryController {
  constructor(
    public service:SearchHistoryService
  ){}

  get base(): CrudController<SearchHistory> {
    return this;
  }

  @ApiBody({
    type: SearchHistory
  })
  @Override()
  createOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: SearchHistory): Promise<SearchHistory> {
    dto.user_id=req.parsed.authPersist.userId;
    dto.add_time=moment().unix();
    return this.base.createOneBase(req, dto);
  }

  @ApiBody({
    type: SearchHistory
  })
  @Override()
  updateOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: any): Promise<SearchHistory> {
    dto.user_id=req.parsed.authPersist.userId
    return this.base.updateOneBase(req, dto);
  }
}
